package org.standard.commons.utils;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {

	public static boolean isStringNull(String str) {
		
		if(str == null || str.trim().equals("")){
			return true;
		}
		return false;
	}
	
	public static String formatDateString(Date date) {
		
		if(date == null || date.equals("")){
			return "";
		}
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}
	public static Date formatStringDate(String str) {
		
		if (isStringNull(str)) {
			return null;
		}
		try {
			return new SimpleDateFormat("yyyy-MM-dd").parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Double formatDoubleNull(Double value) {
		
		if(value == null){
			return 0D;
		}
		return value;
	}
	public static Integer formatIntegerNull(Integer value) {
		
		if(value == null){
			return 0;
		}
		return value;
	}
	public static Integer formatObjToInteger(Object obj) {
		if (obj == null) {
			return 0;
		}
		if (obj instanceof String) {
			String str = (String) obj;
			if (isStringNull(str)) {
				return 0;
			}
			return Integer.valueOf(str);
		}
		if (obj instanceof Integer) {
			return (Integer) obj;
		}
		return Integer.valueOf(obj.toString());
	}
	public static BigDecimal formatBigDecimalNull(BigDecimal value) {
		
		if(value == null){
			return new BigDecimal(0);
		}
		return value;
	}
	public static Double formatStringNull(String value) {
		
		if(isStringNull(value)){
			return 0D;
		}
		return Double.valueOf(value);
	}
	public static boolean equals(Object obj1, Object obj2) {
		
		if (obj1 == null) {
			if (obj2 == null) {
				return true;
			} else {
				return false;
			}
		} else{
			return obj1.equals(obj2);
		}
	}
	public static String getCurrentDateString(String format) {
		
		return new SimpleDateFormat(format).format(new Date());
	}
	public static String divideScale(String top, String bottom) {
		
		if (isStringNull(top) || isStringNull(bottom) || "0".equals(bottom)){
			return "0.00";
		}
		return new BigDecimal(top).multiply(new BigDecimal(100)).divide(new BigDecimal(bottom), 2, BigDecimal.ROUND_HALF_UP).toString();
	}
	/**
	 * 删除文件夹里面的所有文件、单个文件
	 * @param path String 文件夹路径 如 c:/fqf 
	 */
    public static void delFile(String path) { 
    	File file = new File(path); 
	    if (!file.exists()) { 
	       return; 
	    } 
	    if (!file.isDirectory()) { 
	    	if (file.isFile()) {
	    		file.delete();
	    	}
	    	return; 
	    } 
	    String[] tempList = file.list(); 
	    File temp = null; 
	    for (int i = 0; i < tempList.length; i++) { 
	    	if (path.endsWith(File.separator)) { 
	    		temp = new File(path + tempList[i]); 
	    	} else{ 
	    		temp = new File(path + File.separator + tempList[i]); 
	    	} 
	    	if (temp.isFile()) { 
	    		temp.delete(); 
	    	} 
	    	if (temp.isDirectory()) { 
	    		delFile(path + File.separator + tempList[i]);//先删除文件夹里面的文件 
	    	} 
	    }
    } 
    public static void delSimpleFile(String path) { 
    	File file = new File(path); 
	    if (!file.exists()) { 
	       return; 
	    } 
	    if (!file.isDirectory()) { 
	    	if (file.isFile()) {
	    		file.delete();
	    	}
	    } 
    } 
}
